
# -*- coding: utf-8 -*-

# Scrapy settings for commonresources project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:

#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'commonresources'

SPIDER_MODULES = ['commonresources.spiders']
NEWSPIDER_MODULE = 'commonresources.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'commonresources (+http://www.yourdomain.com)'
# 默认的useragent
USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1"

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# DOWNLOAD_DELAY = 1.2
DOWNLOAD_DELAY = 5
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
COOKIES_ENABLED = True
# COOKIES_ENABLED = True

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
# DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
# }

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'commonresources.middlewares.JiangsuSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
    # 'crawlab.pipelines.CrawlabMongoPipeline': 888,
    'commonresources.third_middlewares.IpDaily10MinAgent': 500,
    'commonresources.third_middlewares.IP_DaiLi': 200,
    'commonresources.third_middlewares.AbsoluteSleepMiddleware': 666,
    'commonresources.third_middlewares.RandomUserAgent': 250,
    'commonresources.third_middlewares.DropReferDownloaderMiddlewar': 520,
    'scrapy.downloadermiddlewares.retry.RetryMiddleware': 543,
    'commonresources.third_middlewares.RandomDelayMiddleware': 510 #启动随机延迟中间件
    # 'commonresources.third_middlewares.SeleniumDownloaderMiddleware': 543,
}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    # 'commonresources.pipelines.JiangsuPipeline': 300,
    'scrapy.spidermiddlewares.depth.DepthMiddleware': 299,
    # 'spidertools.common_pipeline.project_html_pipline.ProjectHtmlPipeline': 300, # 已弃用
    'commonresources.project_html_pipline.ProjectHtmlPipeline': 300,
    # 'commonresources.project_html_pipline.HangYeCompanyCountPipeline': 300,


    # 'crawlab.pipelines.CrawlabMongoPipeline':999
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPERROR_ALLOWED_CODES = [301]
# HTTPERROR_ALLOWED_CODES = [301]
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

# 增加错误重试机制

RETRY_ENABLED = True
RETRY_TIMES = 5
DOWNLOAD_TIMEOUT = 2000


# # 20201015 by zhangxing shendu优先
#DEPTH_PRIORITY = 3
#设置广度优先
# 先进先出，广度优先
# DEPTH_PRIORITY = -20
# SCHEDULER_DISK_QUEUE = 'scrapy.squeues.PickleFifoDiskQueue'
# SCHEDULER_MEMORY_QUEUE = 'scrapy.squeues.FifoMemoryQueue'


# 爬虫允许的最大深度，可以通过meta查看当前深度；0表示无深度
# DEPTH_LIMIT = 0

#随机下载延迟，这里延时5s，一般延时是15~90s
RANDOM_DELAY = 5